package com.cmxh.shipper.controller;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.aliyun.oss.OSSClient;
import com.aliyun.teaopenapi.models.Config;
import com.cmxh.shipper.pojo.LegalAuthentication;
import com.cmxh.shipper.service.LegalAuththenticationService;
import com.cmxh.shipper.util.Code;
import com.cmxh.shipper.util.OSSUtil;
import com.cmxh.shipper.util.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.UUID;

/*
 *
 *法人认证信息表Controller
 * */
@RestController
@RequestMapping("/legalAuthentication")
public class LegalAuthenticationcontroller {
    @Autowired
    private LegalAuththenticationService legalAuththenticationService;
    @Autowired
    private OSSUtil ossUtil;

    @RequestMapping("/select/{eid}")
    public Result select(@PathVariable Integer eid) {
        LegalAuthentication legalAuthentication = legalAuththenticationService.selectByPrimaryKey(eid);
        if (legalAuthentication != null) {
            return new Result(Code.GET_OK, legalAuthentication, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @RequestMapping("/shangchuanfaren")
    public Result faren(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String newname = UUID.randomUUID().toString().substring(24) + "-" + originalFilename;
        OSSClient xx1 = new OSSClient(ossUtil.getUrl(), ossUtil.getId(), ossUtil.getMiyao());
        xx1.putObject(ossUtil.getTong(), newname, file.getInputStream());
        String url1 = ossUtil.getUrl();
        String[] split = url1.split("//");
        String url = split[0] + "//" + ossUtil.getTong() + "." + split[1] + "/" + newname;
        RecognizeIdcardRequest pp = new RecognizeIdcardRequest();
        pp.setUrl(url);

        Config config = new Config().setAccessKeyId(ossUtil.getId()).setAccessKeySecret(ossUtil.getMiyao()).setEndpoint("ocr-api.cn-hangzhou.aliyuncs.com");
        // 通过client 向阿里云发送请求
        Client xx = new Client(config);
        //返回的响应
        RecognizeIdcardResponse cc = xx.recognizeIdcard(pp);
        RecognizeIdcardResponseBody body = cc.getBody();
        System.out.println(body.getRequestId());// 请求编号
        System.out.println(body.getCode());// 错误码  一旦识别有问题才会有错误码
        System.out.println(body.getData());// 响应数据  识别成功 就有他
        System.out.println(body.getMessage());// 错误信息 识别有问题才会有错误信息
        // 我们可以把JSON字符串转成Java对象 Java集合 Java数组
        ObjectMapper mapper = new ObjectMapper();
        Map map = mapper.readValue(body.getData(), Map.class);
        System.out.println(map);
        Map data = (Map) map.get("data");
        Map face = (Map) data.get("face");
        if (face != null) {
            Map o = (Map) face.get("data");
            System.out.println(o);
            LegalAuthentication legalAuthentication = new LegalAuthentication();
            legalAuthentication.setLname(o.get("name").toString());
            legalAuthentication.setLidcard(o.get("idNumber").toString());
            legalAuthentication.setLaddress(o.get("address").toString());
            legalAuthentication.setIdCardFrontImagePath(url);
            return new Result(Code.UPDATE_OK, legalAuthentication, "上传成功");
        } else {
            Map back = (Map) data.get("back");
            System.out.println(back);
            Map o = (Map) back.get("data");
            System.out.println(o);
            LegalAuthentication LL = new LegalAuthentication();
            LL.setLqianf(o.get("issueAuthority").toString());
            LL.setLexpiration(o.get("validPeriod").toString());
            LL.setIdCardFrontImagePath(url);

            return new Result(Code.UPDATE_OK, LL, "上传成功");
        }
    }

    @RequestMapping("/addFa")
    public Result addFa(@RequestBody LegalAuthentication legalAuthentication) {
        System.out.println(legalAuthentication);
        int insertall = legalAuththenticationService.insertall(legalAuthentication);
        if (insertall > 0) {
            return new Result(Code.UPDATE_OK, "上传成功");
        } else {
            return new Result(Code.UPDATE_ERR, "上传失败");

        }
    }
}